/*
JETware Hands-free Extension provides additional features for Windows Mobile HFP support.
Copyright (C) 2004-2009 Jim Thatcher

This program is free software; you can redistribute it and/or modify it under the terms of the 
GNU General Public License as published by the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; 
if not, write to the Free Software Foundation, Inc., 
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Source Control header

$Workfile: Messages.cpp $
$Revision: 8 $
$Date: 2/04/07 9:56p $
*/
#include "stdafx.h"
#include <windows.h>
#include "BTHFExt.h"
#include <simmgr.h>
#include <sms.h>

HSIM g_hSIM = NULL;
DWORD g_dwSMSTotal = 0;
DWORD g_dwSMSUsed = 0;

// CHAR g_szNumber[32];
// TODO: Replace this with reading SIM_PBSTORAGE_OWNNUMBERS
BOOL GetMyNumber(CHAR * szNumber, DWORD dwLen)
{
	BOOL bRet = FALSE;
	SMS_ADDRESS addr;
	HRESULT hRes = SmsGetPhoneNumber(&addr);
#ifdef DEBUG
	CHAR szLog[48];
	sprintf(szLog, "SmsGetPhoneNumber returned %8.8X", hRes);
	LogString(szLog, DEBUG_INFO);
#endif
	if (hRes == S_OK)
	{
		CHAR szPhoneNumber[SMS_MAX_ADDRESS_LENGTH];
		sprintf(szPhoneNumber, "%S", addr.ptsAddress);
#ifdef DEBUG
		sprintf(szLog, "PhoneNumber is %s", szPhoneNumber);
		LogString(szLog, DEBUG_INFO);
#endif
		memcpy(szNumber, szPhoneNumber, dwLen);
		bRet = TRUE;
	}
	else
		memset(szNumber, 0, dwLen);
	return bRet;
}

void CleanupSIM()
{
	if (g_hSIM != NULL)
		SimDeinitialize(g_hSIM);
	g_hSIM = NULL;
}

BOOL InitializeSIM()
{
	BOOL bRet = FALSE;
	if (g_hSIM == NULL)
	{
		HRESULT hr = SimInitialize(0, NULL, NULL, &g_hSIM);
		bRet = (hr == S_OK) ? TRUE : FALSE;
	}
	else
		bRet = TRUE;
	return bRet;
}

DWORD GetSIMMessageCount()
{
	HRESULT hr = SimGetSmsStorageStatus(g_hSIM, SIM_SMSSTORAGE_SIM, &g_dwSMSUsed, &g_dwSMSTotal);
	return g_dwSMSUsed;
}

void ReadMessage(int nIndex)
{
	if (InitializeSIM())
	{
		SIMMESSAGE simMessage;

		HRESULT hr = 0;

		simMessage.cbSize = sizeof(SIMMESSAGE);
			
		hr = SimReadMessage(g_hSIM, SIM_SMSSTORAGE_SIM, nIndex, &simMessage);
		if (hr == S_OK)
		{
#ifdef DEBUG
			CHAR szLog[2048];
			CHAR szHeader[1024];
			szHeader[0] = 0;
			for (DWORD i=0;i < simMessage.cbHdrLength; i++)
			{
				CHAR szHexByte[8];
				sprintf(szHexByte, "%2.2X", simMessage.rgbHeader[i]);
				strcat(szHeader, szHexByte);
			}
			sprintf(szLog, "SMS %i from %ls - Header=<%s>, Message=<%ls>", nIndex, simMessage.lpszAddress, szHeader, simMessage.lpszMessage);
			LogString(szLog, DEBUG_MESSAGES);
#endif
		}
		else
		{
#ifdef DEBUG
			CHAR szError[64];
			sprintf(szError, "ERROR reading message %i: SimReadMessage returned %8.8X", nIndex, hr);
			LogString(szError, DEBUG_MESSAGES);
#endif
		}
	}
}

